package com.hmkj.admin.controller.wx;

import com.hmkj.admin.controller.BaseController;
import com.hmkj.admin.controller.GenericController;
import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.entity.dtgrid.DtGrid;
import com.hmkj.core.constant.ConfigNID;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.po.wx.WxAccount;
import com.hmkj.core.po.wx.WxButton;
import com.hmkj.core.service.cache.RedisService;
import com.hmkj.core.service.wx.WxAccountService;
import com.hmkj.core.service.wx.WxButtonService;
import lombok.extern.log4j.Log4j;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by SteveLi on 2016/7/12.
 */
@Log4j
@Controller
@RequestMapping(BaseController.BASE_URI)
public class WxAccountJsonController extends GenericController<Integer, WxAccount> {
    @Resource
    WxAccountService wxAccountService;
    @Resource
    WxButtonService wxButtonService;
    @Resource
    RedisService redisService;

    /**
     * 列表JSON数据
     *
     * @param dtGridPager
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "wxaccount/json/list", method = RequestMethod.POST)
    public DtGrid listJson(String dtGridPager) throws Exception {
        return wxAccountService.getDtGridList(dtGridPager);
    }

    /**
     * 添加一个微信公众号
     *
     * @param wxAccount
     * @param bindingResult
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "wxaccount/json/add", method = RequestMethod.POST)

    public ResultEntity add(WxAccount wxAccount, BindingResult bindingResult) {
        ResultEntity resultEntity = new ResultEntity();
        // 定义返回结果对象
        resultEntity.setUrl(redisService.get(ConfigNID.ADMIN_SERVER_URL) + "wxaccount/list");
        // 格式验证失败
        if (bindingResult.hasErrors()) {
            for (ObjectError error : bindingResult.getAllErrors()) {
                log.error(error.getDefaultMessage());
            }
            resultEntity.setCode(ResultEntity.FAIL);
            resultEntity.setMessage("错误消息");
            return resultEntity;
        }
        wxAccountService.insert(wxAccount);
        //添加自定义菜单的三个一级按钮以及15个子按钮
        WxButton btnone = new WxButton();
        btnone.setWxId(wxAccount.getId());
        btnone.setTopBtn(0);
        wxButtonService.insert(btnone);
        WxButton btntwo = new WxButton();
        btntwo.setWxId(wxAccount.getId());
        btntwo.setTopBtn(0);
        wxButtonService.insert(btntwo);
        WxButton btnthree = new WxButton();
        btnthree.setWxId(wxAccount.getId());
        btnthree.setTopBtn(0);
        wxButtonService.insert(btnthree);
        resultEntity.setMessage("添加成功");
        resultEntity.setCode(ResultEntity.SUCCESS);
        return resultEntity;
    }

    /**
     * 保存微信公众号信息
     *
     * @param wxAccount
     * @param bindingResult
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "wxaccount/json/save", method = RequestMethod.POST)
    public ResultEntity save(WxAccount wxAccount, BindingResult bindingResult) {
        ResultEntity resultEntity = new ResultEntity();
        // 定义返回结果对象
        resultEntity.setUrl(redisService.get(ConfigNID.ADMIN_SERVER_URL) + "wxaccount/list");
        // 格式验证失败
        if (bindingResult.hasErrors()) {
            for (ObjectError error : bindingResult.getAllErrors()) {
                log.error(error.getDefaultMessage());
            }
            resultEntity.setCode(ResultEntity.FAIL);
            resultEntity.setMessage("错误消息");
            return resultEntity;
        }
        wxAccountService.updateByPrimaryKeySelective(wxAccount);
        resultEntity.setMessage("编辑成功");
        resultEntity.setCode(ResultEntity.SUCCESS);
        return resultEntity;
    }

    /**
     * 删除记录
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "wxaccount/json/del", method = RequestMethod.POST)
    public ResultEntity cashDel(@RequestParam(value = "id") Integer id) {
        ResultEntity resultEntity = new ResultEntity();
        if (id <= 0) {
            resultEntity.setCode(ResultEntity.FAIL);
            resultEntity.setMessage("参数错误");
            return resultEntity;
        }
        try {
            wxAccountService.deleteByPrimaryKey(id);
            wxButtonService.deleteByWxId(id);
            resultEntity.setCode(ResultEntity.SUCCESS);
            resultEntity.setMessage("删除成功");
            return resultEntity;
        } catch (BussinessException e) {
            resultEntity.setCode(ResultEntity.FAIL);
            resultEntity.setMessage("删除失败");
            return resultEntity;
        }
    }
}
